package impl

import (
	"context"
	compb "gitee.com/micro-services/protocol/common"
	pb "gitee.com/micro-services/protocol/room/server"
	"micro.room.server/do"
	"micro.room.server/models"
	"strings"
	"time"
)

const (
	Success = "success"
)

func CreateRoom(ctx context.Context, r *pb.CreateRoomReq) (*pb.CreateRoomRsp, error) {
	room := &models.VsHRoom{
		HotelId:           r.Info.HotelID,
		Floor:             r.Info.Floor,
		Room:              r.Info.RoomNo,
		Image:             r.Info.RoomImage,
		SmartImage:        r.Info.SmartImage,
		DetailImage:       strings.Join(r.Info.DetailImages, ","),
		Detail:            r.Info.Detail,
		Price:             r.Info.Price,
		MarketPrice:       r.Info.MarketPrice,
		ActivityPrice:     r.Info.ActivityPrice,
		RoomType:          r.Info.RoomType,
		ToiletNum:         r.Info.ToiletNum,
		BathroomDevice:    r.Info.BathroomDevice,
		ConvenienceDevice: r.Info.ConvenienceDevice,
		OtherDevice:       r.Info.OtherDevice,
		Source:            r.Info.Source,
		PackageId:         r.Info.PackageID,
		IsOpen:            r.Info.IsOpen,
		CreatedTime:       time.Now().Unix(),
		UpdatedTime:       time.Now().Unix(),
	}
	err := do.CreateRoom(room)
	if err != nil {
		return nil, err
	}
	return &pb.CreateRoomRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func UpdateRoom(ctx context.Context, r *pb.UpdateRoomReq) (*pb.UpdateRoomRsp, error) {
	room := &models.VsHRoom{
		RoomId:            r.RoomID,
		Floor:             r.Floor,
		Room:              r.RoomNo,
		Image:             r.RoomImage,
		SmartImage:        r.SmartImage,
		DetailImage:       strings.Join(r.DetailImages, ","),
		Detail:            r.Detail,
		Price:             r.Price,
		MarketPrice:       r.MarketPrice,
		ActivityPrice:     r.ActivityPrice,
		RoomType:          r.RoomType,
		ToiletNum:         r.ToiletNum,
		BathroomDevice:    r.BathroomDevice,
		ConvenienceDevice: r.ConvenienceDevice,
		OtherDevice:       r.OtherDevice,
		IsOpen:            r.IsOpen,
		UpdatedTime:       time.Now().Unix(),
	}
	err := do.UpdateRoom(room)
	if err != nil {
		return nil, err
	}

	return &pb.UpdateRoomRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func DeleteRoom(ctx context.Context, r *pb.DeleteRoomReq) (*pb.DeleteRoomRsp, error) {
	err := do.DeleteRoom(r.RoomID)
	if err != nil {
		return nil, err
	}

	return &pb.DeleteRoomRsp{
		RetCode: 0,
		Msg:     Success,
	}, nil
}

func BatchRoomInfo(ctx context.Context, r *pb.BatchRoomInfoReq) ([]*compb.RoomInfo, error) {
	rooms, err := do.BatchRoomInfo(r.RoomID)
	if err != nil {
		return nil, err
	}

	infos := make([]*compb.RoomInfo, 0)
	for _, v := range rooms {
		infos = append(infos, &compb.RoomInfo{
			RoomID:            v.RoomId,
			HotelID:           v.HotelId,
			Floor:             v.Floor,
			RoomNo:            v.Room,
			RoomImage:         v.Image,
			SmartImage:        v.SmartImage,
			DetailImages:      strings.Split(v.DetailImage, ","),
			Detail:            v.Detail,
			Price:             v.Price,
			MarketPrice:       v.MarketPrice,
			ActivityPrice:     v.ActivityPrice,
			RoomType:          v.RoomType,
			ToiletNum:         v.ToiletNum,
			BathroomDevice:    v.BathroomDevice,
			ConvenienceDevice: v.ConvenienceDevice,
			OtherDevice:       v.OtherDevice,
			Source:            v.Source,
			PackageID:         v.PackageId,
			IsOpen:            v.IsOpen,
		})
	}

	return infos, nil
}

func SearchRoom(ctx context.Context, r *pb.SearchRoomReq) ([]*compb.RoomInfo, int64, error) {
	rooms, count, err := do.SearchRoom(&models.SearchRoomParams{
		HotelID:       r.HotelID,
		Floor:         r.Floor,
		RoomNo:        r.RoomNo,
		Price:         r.Price,
		MarketPrice:   r.MarketPrice,
		ActivityPrice: r.ActivityPrice,
		RoomType:      r.RoomType,
		IsOpen:        r.IsOpen,
		Offset:        r.Offset,
		Limit:         r.Limit,
	})
	if err != nil {
		return nil, 0, err
	}

	infos := make([]*compb.RoomInfo, 0)
	for _, v := range rooms {
		infos = append(infos, &compb.RoomInfo{
			RoomID:            v.RoomId,
			HotelID:           v.HotelId,
			Floor:             v.Floor,
			RoomNo:            v.Room,
			RoomImage:         v.Image,
			SmartImage:        v.SmartImage,
			DetailImages:      strings.Split(v.DetailImage, ","),
			Detail:            v.Detail,
			Price:             v.Price,
			MarketPrice:       v.MarketPrice,
			ActivityPrice:     v.ActivityPrice,
			RoomType:          v.RoomType,
			ToiletNum:         v.ToiletNum,
			BathroomDevice:    v.BathroomDevice,
			ConvenienceDevice: v.ConvenienceDevice,
			OtherDevice:       v.OtherDevice,
			Source:            v.Source,
			PackageID:         v.PackageId,
			IsOpen:            v.IsOpen,
		})
	}

	return infos, count, nil
}
